﻿namespace Dcms.UpdateModule
{
    using Dcms.Common;
    using System;
    using System.Collections;
    using System.Data;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Configuration;

    public class DcmsIntroProcessCode
    {
        private static readonly string DataBaseType = ConfigurationManager.AppSettings["DAL"];

        public DataTable IntroList(string SqlStr)
        {
            string cateId = "";
            ArrayList paraNameArr = new ArrayList();
            ArrayList constDefaultvalueArr = new ArrayList();
            string[] strArray = SqlStr.Split(new char[] { '$' });
            string tempSqlStr = strArray[0];
            string pageSize = strArray[1];
            string selectDir = strArray[3];
            string[] strArray2 = strArray[4].Split(new char[] { ',' });
            paraNameArr = DcmsCommSqlProcess.GetParaNameArr(strArray[0], ref constDefaultvalueArr);
            if (paraNameArr.Count > 0)
            {
                for (int i = 0; i < paraNameArr.Count; i++)
                {
                    string str5;
                    string str7 = paraNameArr[i].ToString().ToLower();
                    if (str7 != null)
                    {
                        if (!(str7 == "introid"))
                        {
                            if (str7 == "introcateid") goto Label_022B;
                            if (str7 == "keyword") goto Label_0471;
                        }
                        else if (strArray2[i].ToLower() == "con")
                            tempSqlStr = tempSqlStr.Replace(string.Concat(new object[] { "[", paraNameArr[i], "#", constDefaultvalueArr[i], "]" }), constDefaultvalueArr[i].ToString());
                        else if (DcmsRequest.GetQueryInt("IntroId", 0) != 0)
                            tempSqlStr = tempSqlStr.Replace(string.Concat(new object[] { "[", paraNameArr[i], "#", constDefaultvalueArr[i], "]" }), HttpContext.Current.Request.QueryString["IntroId"].ToString());
                        else
                            tempSqlStr = tempSqlStr.Replace(string.Concat(new object[] { "[", paraNameArr[i], "#", constDefaultvalueArr[i], "]" }), constDefaultvalueArr[i].ToString());
                    }
                    continue;
                Label_022B:
                    if (strArray2[i].ToLower() == "con")
                        tempSqlStr = tempSqlStr.Replace(string.Concat(new object[] { "[", paraNameArr[i], "#", constDefaultvalueArr[i], "]" }), "(" + DcmsCommSqlProcess.GetCateListInfo(constDefaultvalueArr[i].ToString(), selectDir) + ")");
                    else if (DcmsRequest.GetQueryInt("IntroCateId", 0) != 0)
                        tempSqlStr = tempSqlStr.Replace(string.Concat(new object[] { "[", paraNameArr[i], "#", constDefaultvalueArr[i], "]" }), "(" + DcmsCommSqlProcess.GetCateListInfo(HttpContext.Current.Request.QueryString["IntroCateId"].ToString(), selectDir) + ")");
                    else if (DcmsRequest.ValidaQueryStr("IntroCateId"))
                        tempSqlStr = tempSqlStr.Replace(string.Concat(new object[] { "[", paraNameArr[i], "#", constDefaultvalueArr[i], "]" }), "(" + DcmsCommSqlProcess.GetCateListInfo(Utils.check_TxtSql(HttpContext.Current.Request.QueryString["IntroCateId"].ToString()), selectDir) + ")");
                    else
                        tempSqlStr = tempSqlStr.Replace(string.Concat(new object[] { "[", paraNameArr[i], "#", constDefaultvalueArr[i], "]" }), "(" + DcmsCommSqlProcess.GetCateListInfo(constDefaultvalueArr[i].ToString(), selectDir) + ")");
                    continue;
                Label_0471:
                    str5 = "";
                    if (strArray2[i].ToLower() == "con")
                        str5 = constDefaultvalueArr[i].ToString();
                    else if (DcmsRequest.ValidaQueryStr("KeyWord"))
                        str5 = HttpContext.Current.Request.QueryString["KeyWord"].ToString();
                    else
                        str5 = constDefaultvalueArr[i].ToString();
                    str5 = DcmsCommSqlProcess.FormatSql("'%" + str5 + "%'");
                    tempSqlStr = tempSqlStr.Replace(string.Concat(new object[] { "[", paraNameArr[i], "#", constDefaultvalueArr[i], "]" }), str5);
                }
            }
            else
                tempSqlStr = tempSqlStr.Replace("(" + cateId + ")", "(" + DcmsCommSqlProcess.GetCateListInfo(cateId, selectDir) + ")");
            return DcmsCommSqlProcess.ReturnTableData(tempSqlStr, pageSize, null);
        }

        public string IntroModuleSql(string ModelName, string FileList, string ParaName, string DefaultValue, string SelectDir, string ConditionStr, string NumStr, string IsPage, string OrderFileStr, string SortStr, string ParaTypeStr)
        {
            string[] strArray3;
            int num2;
            string str = "";
            string str2 = "";
            if (NumStr == "") NumStr = "1";
            if (OrderFileStr == "") OrderFileStr = "Intro_Id";
            if (SortStr == "") SortStr = "desc";
            if (IsPage == "") IsPage = "false";
            if (ParaTypeStr == "") ParaTypeStr = "con";
            string[] strArray = ParaTypeStr.Split(new char[] { ',' });
            string str3 = ParaTypeStr;
            if (ParaName != "")
            {
                string[] strArray2 = ParaName.Split(new char[] { ',' });
                strArray3 = DefaultValue.Split(new char[] { '|' });
                string str4 = "";
                for (num2 = 0; num2 < strArray2.Length; num2++)
                {
                    string str7 = strArray2[num2].ToLower();
                    if (str7 != null)
                    {
                        if (!(str7 == "introid"))
                        {
                            if (str7 == "introcateid") goto Label_01BE;
                            if (str7 == "keyword") goto Label_021A;
                        }
                        else
                        {
                            if (strArray3.Length > num2)
                                str4 = strArray3[num2];
                            else
                                str4 = "1";
                            if (strArray.Length < num2) str3 = str3 + strArray[strArray.Length - 1];
                            str = str + "Intro_Id=[IntroId#" + str4 + "] and ";
                        }
                    }
                    continue;
                Label_01BE:
                    if (strArray3.Length > num2)
                        str4 = strArray3[num2];
                    else
                        str4 = "1";
                    if (strArray.Length < num2) str3 = str3 + strArray[strArray.Length - 1];
                    str = str + "Intro_CateId in [IntroCateId#" + str4 + "] and ";
                    continue;
                Label_021A:
                    if (strArray3.Length > num2)
                        str4 = strArray3[num2];
                    else
                        str4 = "1";
                    if (strArray.Length < num2) str3 = str3 + strArray[strArray.Length - 1];
                    str = str + "Intro_Title like [KeyWord#" + str4 + "] and ";
                }
            }
            else
            {
                strArray3 = DefaultValue.Split(new char[] { ',' });
                string str5 = "";
                for (num2 = 0; num2 < strArray3.Length; num2++)
                {
                    str5 = str5 + strArray3[num2] + ",";
                }
                if (str5.Length > 0)
                    str5 = str5.Substring(0, str5.Length - 1);
                else
                    str5 = "0";
                str = str + "Intro_CateId in(" + str5 + ") and ";
            }
            if (str.Length > 4)
                str = str + "Intro_State='1'";
            else
                str = "Intro_State='1'";
            if (IsPage.ToLower() == "true")
            {
                str2 = "select " + FileList + " from Dcms_Intro where " + str + " order by " + OrderFileStr + " " + SortStr;
                return (str2 + "$" + NumStr + "$" + ModelName + "$" + SelectDir + "$" + str3);
            }
            str2 = "select top " + NumStr + " " + FileList + " from Dcms_Intro where " + str + " order by " + OrderFileStr + " " + SortStr;
            return (str2 + "$0$" + ModelName + "$" + SelectDir + "$" + str3);
        }

        public string SpecialFieldResolve(string HtmlTempLate, string ModelName, DataRow Datadr)
        {
            string str = HtmlTempLate;
            string replacement = "";
            MatchCollection matchs = new Regex("{(?<SpeFile>(" + ModelName + @".\w+?.\w+)),?(?<Other>(\w*?))}").Matches(HtmlTempLate);
            for (int i = 0; i < matchs.Count; i++)
            {
                string str11 = matchs[i].Groups["SpeFile"].Value.ToLower();
                if (str11 != null)
                {
                    if (!(str11 == "intro.cateid.name"))
                    {
                        if (str11 == "intro.content.page") goto Label_011D;
                    }
                    else
                    {
                        replacement = DcmsDataBaseHelper.GetOneFileValue("select Cate_Name from Dcms_Cate where Cate_ID=" + Datadr["Intro_CateId"].ToString(), "未知分类");
                        string str5 = "";
                        if (matchs[i].Groups["Other"].Value.ToLower() == "")
                        {
                            str5 = "{intro.cateid.name}";
                            HtmlTempLate = Regex.Replace(HtmlTempLate, str5, replacement, RegexOptions.IgnoreCase);
                        }
                        else
                        {
                            str5 = @"{intro.cateid.name,(?<Len>(\d*)?)}";
                            HtmlTempLate = DcmsCommSqlProcess.FarmatStringLenght(str5, HtmlTempLate, replacement);
                        }
                        str = HtmlTempLate;
                    }
                }
                continue;
            Label_011D:
                replacement = Utils.setPage(Datadr["Intro_Content"].ToString());
                string pattern = "";
                if (matchs[i].Groups["Other"].Value.ToLower() == "")
                {
                    pattern = "{intro.content.page}";
                    HtmlTempLate = Regex.Replace(HtmlTempLate, pattern, replacement, RegexOptions.IgnoreCase);
                }
                else
                {
                    pattern = @"{intro.content.page,(?<Len>(\d*)?)}";
                    HtmlTempLate = DcmsCommSqlProcess.FarmatStringLenght(pattern, HtmlTempLate, replacement);
                }
                str = HtmlTempLate;
            }
            return str;
        }
    }
}

